home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / ACROYNMity / file.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.9 KB  |  374 lines

  1. // Acronymity
  2. // © 2000 by Karl Kornel (kornel.1@osu.edu)
  3. // Made during MacHack 2000
  4. // Changes & Redistribution allowed is credit given
  5.  
  6. /* The headers we need */
  7. #include <iostream.h>
  8. #include <fstream.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. /* The User-Defined Settings */
  14. #include "settings.h"
  15.  
  16. /* The prototypes for the functions in this file */
  17. bool openFiles(void);
  18. int getWord(char *, const char);
  19. void getTheWord(char *, const char, const int);
  20. void openError(const char);
  21. void moveToStart(void);
  22. void closeFiles(void);
  23.  
  24. /* Prototypes for any non-local functions */
  25. int getLetterCode(const char);
  26.  
  27. /* Global variables */
  28. int lineUsed[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  29.  
  30. /* The file streams */
  31.     ifstream index;
  32.     ifstream a;
  33.     ifstream b;
  34.     ifstream c;
  35.     ifstream d;
  36.     ifstream e;
  37.     ifstream f;
  38.     ifstream g;
  39.     ifstream h;
  40.     ifstream i;
  41.     ifstream j;
  42.     ifstream k;
  43.     ifstream l;
  44.     ifstream m;
  45.     ifstream n;
  46.     ifstream o;
  47.     ifstream p;
  48.     ifstream q;
  49.     ifstream r;
  50.     ifstream s;
  51.     ifstream t;
  52.     ifstream u;
  53.     ifstream v;
  54.     ifstream w;
  55.     ifstream x;
  56.     ifstream y;
  57.     ifstream z;
  58.  
  59. /* The code */
  60. bool openFiles(void) { // Open out files
  61.     bool openStatus = true; // Assume that all is well
  62.     
  63.     index.open("index.dat",ios::in);
  64.     if (!index) openError('\0');
  65.     a.open("a.txt",ios::in);
  66.     if (!a) openError('a');
  67.     b.open("b.txt",ios::in);
  68.     if (!b) openError('b');
  69.     c.open("c.txt",ios::in);
  70.     if (!c) openError('c');
  71.     d.open("d.txt",ios::in);
  72.     if (!d) openError('d');
  73.     e.open("e.txt",ios::in);
  74.     if (!e) openError('e');
  75.     f.open("f.txt",ios::in);
  76.     if (!f) openError('f');
  77.     g.open("g.txt",ios::in);
  78.     if (!g) openError('g');
  79.     h.open("h.txt",ios::in);
  80.     if (!h) openError('h');
  81.     i.open("i.txt",ios::in);
  82.     if (!i) openError('i');
  83.     j.open("j.txt",ios::in);
  84.     if (!j) openError('j');
  85.     k.open("k.txt",ios::in);
  86.     if (!k) openError('k');
  87.     l.open("l.txt",ios::in);
  88.     if (!l) openError('l');
  89.     m.open("m.txt",ios::in);
  90.     if (!m) openError('m');
  91.     n.open("n.txt",ios::in);
  92.     if (!n) openError('n');
  93.     o.open("o.txt",ios::in);
  94.     if (!o) openError('o');
  95.     p.open("p.txt",ios::in);
  96.     if (!p) openError('p');
  97.     q.open("q.txt",ios::in);
  98.     if (!q) openError('q');
  99.     r.open("r.txt",ios::in);
  100.     if (!r) openError('r');
  101.     s.open("s.txt",ios::in);
  102.     if (!s) openError('s');
  103.     t.open("t.txt",ios::in);
  104.     if (!t) openError('t');
  105.     u.open("u.txt",ios::in);
  106.     if (!u) openError('u');
  107.     v.open("v.txt",ios::in);
  108.     if (!v) openError('v');
  109.     w.open("w.txt",ios::in);
  110.     if (!w) openError('w');
  111.     x.open("x.txt",ios::in);
  112.     if (!x) openError('x');
  113.     y.open("y.txt",ios::in);
  114.     if (!y) openError('y');
  115.     z.open("z.txt",ios::in);
  116.     if (!z) openError('z');
  117.     
  118.     return openStatus;
  119. }
  120.  
  121. int getWord(char *wordSpace, const char letter) { // Get a word from a file
  122.     /* Variables */
  123.     char letterComma = '\0'; // One character for the letter, and one comma
  124.     int letterEntries; // The # of entries in the line (for a letter)
  125.     int chosenLine = 1; // The line from which we will pull text
  126.     int theWordLength = 0; // The length of the word
  127.     int whereAreWeNow = 1; // The character of the word we're looking at now
  128.     
  129.     /* Keep searching until we find the correct letter (and it's # of words) */
  130.     while (letterComma != letter) // As long as we can't find the right letter
  131.         index >> letterComma >> letterEntries; // Pull in another line
  132.     
  133.     /* Now, let's pull the random word from the correct file */
  134.     getTheWord(wordSpace,letter,letterEntries);
  135.     
  136.     /* Now, let's find out the word's length */
  137.     bool continueSearching = true; // Keep looking
  138.     while (continueSearching == true) {
  139.         if (wordSpace[whereAreWeNow - 1] != '\0') 
  140.             whereAreWeNow = whereAreWeNow + 1; // Move forward one character
  141.         else
  142.             continueSearching = false; // We can stop searching
  143.     }
  144.     
  145.     /* Go back to the beginning */
  146.     moveToStart();
  147.     
  148.     return whereAreWeNow - 1; // Return the length of the word (minux the /0)
  149. }
  150.  
  151. void getTheWord(char *wordSpace,const char letter, const int letterEntries) {
  152.     int chosenLine = 1;
  153.     
  154.     srand(time(NULL)); // Seed the random-number system
  155.     while (true) { // Repeat forever (until we break out)
  156.         chosenLine = 1 + (rand() % letterEntries); // Choose the line
  157.         if (chosenLine != lineUsed[getLetterCode(letter)]) { // If it doesn't match
  158.             lineUsed[getLetterCode(letter)] = chosenLine;
  159.             break; // Break out of the loop    
  160.         } else if (letterEntries == 1) // If there's only one word for this letter
  161.             break; // Break out of the loop
  162.     }
  163.     
  164.     switch (letter) {
  165.     case 'a':
  166.         for (int temp=1;temp < chosenLine;temp++)
  167.             a.ignore(wordLength + 1, '\n'); // Ignore the line
  168.         a >> wordSpace; // Pull in the line text
  169.         break; // Get out of the structure
  170.     case 'b':
  171.         for (int temp=1;temp < chosenLine;temp++)
  172.             b.ignore(wordLength + 1, '\n'); // Ignore the line
  173.         b >> wordSpace;
  174.         break; // Get out of the structure
  175.     case 'c':
  176.         for (int temp=1;temp < chosenLine;temp++)
  177.             c.ignore(wordLength + 1, '\n'); // Ignore the line
  178.         c >> wordSpace;
  179.         break; // Get out of the structure
  180.     case 'd':
  181.         for (int temp=1;temp < chosenLine;temp++)
  182.             d.ignore(wordLength + 1, '\n'); // Ignore the line
  183.         d >> wordSpace;
  184.         break; // Get out of the structure
  185.     case 'e':
  186.         for (int temp=1;temp < chosenLine;temp++)
  187.             e.ignore(wordLength + 1, '\n'); // Ignore the line
  188.         e >> wordSpace;
  189.         break; // Get out of the structure
  190.     case 'f':
  191.         for (int temp=1;temp < chosenLine;temp++)
  192.             f.ignore(wordLength + 1, '\n'); // Ignore the line
  193.         f >> wordSpace;
  194.         break; // Get out of the structure
  195.     case 'g':
  196.         for (int temp=1;temp < chosenLine;temp++)
  197.             g.ignore(wordLength + 1, '\n'); // Ignore the line
  198.         g >> wordSpace;
  199.         break; // Get out of the structure
  200.     case 'h':
  201.         for (int temp=1;temp < chosenLine;temp++)
  202.             h.ignore(wordLength + 1, '\n'); // Ignore the line
  203.         h >> wordSpace;
  204.         break; // Get out of the structure
  205.     case 'i':
  206.         for (int temp=1;temp < chosenLine;temp++)
  207.             i.ignore(wordLength + 1, '\n'); // Ignore the line
  208.         i >> wordSpace;
  209.         break; // Get out of the structure
  210.     case 'j':
  211.         for (int temp=1;temp < chosenLine;temp++)
  212.             j.ignore(wordLength + 1, '\n'); // Ignore the line
  213.         j >> wordSpace;
  214.         break; // Get out of the structure
  215.     case 'k':
  216.         for (int temp=1;temp < chosenLine;temp++)
  217.             k.ignore(wordLength + 1, '\n'); // Ignore the line
  218.         k >> wordSpace;
  219.         break; // Get out of the structure
  220.     case 'l':
  221.         for (int temp=1;temp < chosenLine;temp++)
  222.             l.ignore(wordLength + 1, '\n'); // Ignore the line
  223.         l >> wordSpace;
  224.         break; // Get out of the structure
  225.     case 'm':
  226.         for (int temp=1;temp < chosenLine;temp++)
  227.             m.ignore(wordLength + 1, '\n'); // Ignore the line
  228.         m >> wordSpace;
  229.         break; // Get out of the structure
  230.     case 'n':
  231.         for (int temp=1;temp < chosenLine;temp++)
  232.             n.ignore(wordLength + 1, '\n'); // Ignore the line
  233.         n >> wordSpace;
  234.         break; // Get out of the structure
  235.     case 'o':
  236.         for (int temp=1;temp < chosenLine;temp++)
  237.             o.ignore(wordLength + 1, '\n'); // Ignore the line
  238.         o >> wordSpace;
  239.         break; // Get out of the structure
  240.     case 'p':
  241.         for (int temp=1;temp < chosenLine;temp++)
  242.             p.ignore(wordLength + 1, '\n'); // Ignore the line
  243.         p >> wordSpace;
  244.         break; // Get out of the structure
  245.     case 'q':
  246.         for (int temp=1;temp < chosenLine;temp++)
  247.             q.ignore(wordLength + 1, '\n'); // Ignore the line
  248.         q >> wordSpace;
  249.         break; // Get out of the structure
  250.     case 'r':
  251.         for (int temp=1;temp < chosenLine;temp++)
  252.             r.ignore(wordLength + 1, '\n'); // Ignore the line
  253.         r >> wordSpace;
  254.         break; // Get out of the structure
  255.     case 's':
  256.         for (int temp=1;temp < chosenLine;temp++)
  257.             s.ignore(wordLength + 1, '\n'); // Ignore the line
  258.         s >> wordSpace;
  259.         break; // Get out of the structure
  260.     case 't':
  261.         for (int temp=1;temp < chosenLine;temp++)
  262.             t.ignore(wordLength + 1, '\n'); // Ignore the line
  263.         t >> wordSpace;
  264.         break; // Get out of the structure
  265.     case 'u':
  266.         for (int temp=1;temp < chosenLine;temp++)
  267.             u.ignore(wordLength + 1, '\n'); // Ignore the line
  268.         u >> wordSpace;
  269.         break; // Get out of the structure
  270.     case 'v':
  271.         for (int temp=1;temp < chosenLine;temp++)
  272.             v.ignore(wordLength + 1, '\n'); // Ignore the line
  273.         v >> wordSpace;
  274.         break; // Get out of the structure
  275.     case 'w':
  276.         for (int temp=1;temp < chosenLine;temp++)
  277.             w.ignore(wordLength + 1, '\n'); // Ignore the line
  278.         w >> wordSpace;
  279.         break; // Get out of the structure
  280.     case 'x':
  281.         for (int temp=1;temp < chosenLine;temp++)
  282.             x.ignore(wordLength + 1, '\n'); // Ignore the line
  283.         x >> wordSpace;
  284.         break; // Get out of the structure
  285.     case 'y':
  286.         for (int temp=1;temp < chosenLine;temp++)
  287.             y.ignore(wordLength + 1, '\n'); // Ignore the line
  288.         y >> wordSpace;
  289.         break; // Get out of the structure
  290.     case 'z':
  291.         for (int temp=1;temp < chosenLine;temp++)
  292.             z.ignore(wordLength + 1, '\n'); // Ignore the line
  293.         z >> wordSpace;
  294.         break; // Get out of the structure
  295.     default:
  296.         for (int temp=1;temp <= 29;temp++)
  297.             cout << '\n';
  298.         cout << endl << "ERROR!  Unable to find correct file." << endl;
  299.         exit(-37);
  300.     }
  301. }
  302.  
  303. void openError(const char letter) { // Handle file-open errors
  304.     for (int temp = 1;temp <= 29;temp++)
  305.         cout << "\n";
  306.     cout << endl;
  307.     
  308.     cout << "ERROR!\n\n"
  309.         << "The file " << letter << ".txt does not exist, please make one." << endl;
  310.     
  311.     sleep(1);
  312.     
  313.     exit(-130);
  314. }
  315.  
  316. void moveToStart(void) {
  317.     index.seekg(0,ios::beg);
  318.     a.seekg(0,ios::beg);
  319.     b.seekg(0,ios::beg);
  320.     c.seekg(0,ios::beg);
  321.     d.seekg(0,ios::beg);
  322.     e.seekg(0,ios::beg);
  323.     f.seekg(0,ios::beg);
  324.     g.seekg(0,ios::beg);
  325.     h.seekg(0,ios::beg);
  326.     i.seekg(0,ios::beg);
  327.     j.seekg(0,ios::beg);
  328.     k.seekg(0,ios::beg);
  329.     l.seekg(0,ios::beg);
  330.     m.seekg(0,ios::beg);
  331.     n.seekg(0,ios::beg);
  332.     o.seekg(0,ios::beg);
  333.     p.seekg(0,ios::beg);
  334.     q.seekg(0,ios::beg);
  335.     r.seekg(0,ios::beg);
  336.     s.seekg(0,ios::beg);
  337.     t.seekg(0,ios::beg);
  338.     u.seekg(0,ios::beg);
  339.     v.seekg(0,ios::beg);
  340.     w.seekg(0,ios::beg);
  341.     x.seekg(0,ios::beg);
  342.     y.seekg(0,ios::beg);
  343.     z.seekg(0,ios::beg);
  344. }
  345.  
  346. void closeFiles(void) { // Close our files
  347.     index.close();
  348.     a.close();
  349.     b.close();
  350.     c.close();
  351.     d.close();
  352.     e.close();
  353.     f.close();
  354.     g.close();
  355.     h.close();
  356.     i.close();
  357.     j.close();
  358.     k.close();
  359.     l.close();
  360.     m.close();
  361.     n.close();
  362.     o.close();
  363.     p.close();
  364.     q.close();
  365.     r.close();
  366.     s.close();
  367.     t.close();
  368.     u.close();
  369.     v.close();
  370.     w.close();
  371.     x.close();
  372.     y.close();
  373.     z.close();
  374. }